package com.cookman.handlers;

import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.Camera;
import com.cookman.renderers.BaseRenderer;

/**
 * Created by Cookman on 18.09.14.
 */
public class CustomInputProcessor implements InputProcessor {

    private float zoom = 1.0f;
    private Camera camera;
    private BaseRenderer worldRenderer;

    public CustomInputProcessor(Camera camera, BaseRenderer worldRenderer) {
        this.camera = camera;
        this.worldRenderer = worldRenderer;
    }

    @Override
    public boolean scrolled(int amount) {
        //Zoom out
        if (amount > 0 && zoom < 1) {
            zoom += 0.1f;
        }

        //Zoom in
        if (amount < 0 && zoom > 0.1) {
            zoom -= 0.1f;
        }

        worldRenderer.updateZoom(zoom);
        return true;
    }

    @Override
    public boolean keyDown(int i) {
        return false;
    }

    @Override
    public boolean keyUp(int i) {
        return false;
    }

    @Override
    public boolean keyTyped(char c) {
        return false;
    }

    float deltaX = 0.06f;
    float deltaY = 0.06f;

    @Override
    public boolean touchDown(int i, int i2, int i3, int i4) {
        /*deltaX = 0.01f;
        deltaY = 0.01f;*/
       // oldX = 0;
       // oldY = 0;
        return false;
    }

    @Override
    public boolean touchUp(int i, int i2, int i3, int i4) {
        /*deltaX = 0.01f;
        deltaY = 0.01f;*/
        oldX = 0;
        oldY = 0;
        return false;
    }

    int oldX = 0;
    int oldY = 0;

    @Override
    public boolean touchDragged(int i, int i2, int i3) {

        //  if(!((AliceUI) ServiceLocator.getUIController()).canSelectCities() || !ServiceLocator.getWorldRenderer().isModalVisible()) return false;
        float widthDelta = 5;

        float deltaX = 0.08f*zoom;
        float deltaY = 0.08f*zoom;
        /*if (Math.abs(deltaX) <= 0.08f) {
            deltaX = (Math.abs(deltaX) + 0.005f);
        } else {
            deltaX = 0.08f;
        }
        if (Math.abs(deltaY) <= 0.08f) {
            deltaY = (Math.abs(deltaY) + 0.005f);
        } else {
            deltaY = 0.08f;
        }*/
        Boolean needMoveX = false;
        Boolean needMoveY = false;
      //  if (oldX != 0 && oldY != 0) {

            if (oldX != 0 && Math.abs(i - oldX) > widthDelta) {
                needMoveX = true;
            }

            if (oldY!=0 && Math.abs(i2 - oldY) > widthDelta) {
                needMoveY = true;
            }

        //System.out.println(oldX+";"+i+";"+oldY+";"+i2);
            if (needMoveX) {
                if (i < oldX) {
                    camera.position.x += deltaX;
                } else {
                    deltaX = -deltaX;
                    camera.position.x += deltaX;
                }
            }
            if (needMoveY) {
                if (i2 > oldY) {
                    camera.position.y += deltaY;
                } else {
                    deltaY = -deltaY;
                    camera.position.y += deltaY;
                }
            }
     //   }
        camera.update();
        oldX = i;
        oldY = i2;
        return false;
    }

    @Override
    public boolean mouseMoved(int i, int i2) {
        return false;
    }
}

